
import java.net.Socket;
import java.net.ServerSocket;
import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.io.*;



public class Server implements Runnable{
	
	private static int PORT = 7789;
	private static int MAX_CLIENTS = 800;
	
	private ServerSocket serverSock;	
	private MySorter sorter;	
	private HistoryData history;
	private ExecutorService threadPool;
	
	private HashMap<Integer, Integer> stations;

	public Server(MySorter sorter, HashMap<Integer, Integer> stations, HistoryData history) {
		this.sorter = sorter;
		this.stations = stations;
		this.history = history;
		threadPool = Executors.newFixedThreadPool(MAX_CLIENTS);
	}


	@Override
	public void run() {	
		try {
			serverSock = new ServerSocket(PORT);			
			while (true) {							
				Socket sock = serverSock.accept();					
				ClientHandler client = new ClientHandler(sock, sorter, stations, history);
				threadPool.execute(client);			
			}	
		}
		catch (IOException e){
			e.printStackTrace(System.err);
		}
	}
}